<template>
	<view class="content">
		<view class="form">
			<view class="titel">
				派工信息
			</view>
			<view class="itembox">
				<view class="name">
					分配任务时间:
				</view>
				<view class="inputBox">
					<input type="number" v-model="info.createTime" disabled="" />
				</view>
			</view>
			<view class="itembox"  @click="areasPopupBnt()">
				<view class="name">
					工人:
				</view>
				<view class="inputBox">
					<input type="number" v-model="selectedWorker[0].selectName" disabled="" />
				</view>
			</view>
			<view class="itembox">
				<view class="name">
					生产订单号:
				</view>
				<view class="inputBox">
					<input type="number" v-model="info.prodNo" placeholder="请填写生产订单号" disabled="" />
				</view>
			</view>
			<view class="itembox">
				<view class="name">
					工序名称:
				</view>
				<view class="inputBox">
					<input type="number" v-model="info.name" placeholder="请填写工序名称" disabled="" />
				</view>
			</view>
			<view class="itembox">
				<view class="name">
					产品名称:
				</view>
				<view class="inputBox">
					<input type="number" v-model="info.goodsName" placeholder="请填写产品名称" disabled="" />
				</view>
			</view>
			<view class="itembox">
				<view class="name">
					产品型号:
				</view>
				<view class="inputBox">
					<input type="number" v-model="info.goodsModel" placeholder="请填写产品型号" disabled="" />
				</view>
			</view>
			<view class="itembox" v-if="info.gongxuName">
				<view class="name">
					生产产品名称:
				</view>
				<view class="inputBox">
					<input type="number" v-model="info.gongxuName" placeholder="请填写生产产品名称" disabled="" />
				</view>
			</view>
			<view class="itembox" v-if="info.gongxuModel">
				<view class="name" >
					生产产品型号:
				</view>
				<view class="inputBox">
					<input type="number" v-model="info.gongxuModel" placeholder="请填写生产产品型号" disabled="" />
				</view>
			</view>
			<view class="itembox">
				<view class="name">
					分配数量:
				</view>
				<view class="inputBox">
					<input type="number" v-model="info.count" @input="onKeyInput" placeholder="请填写分配数量" />
				</view>
			</view>
			<view class="itembox">
				<view class="name">
					剩余完成数量:
				</view>
				<view class="inputBox">
					<input type="number" disabled="" v-model="info.remainCount" placeholder="请填写剩余完成数量"/>
				</view>
			</view>
			<view class="itembox" v-if="info.money">
				<view class="name">
					单价:
				</view>
				<view class="inputBox">
					<input type="number" disabled="" v-model="info.money" placeholder="请填写单价"/>
				</view>
			</view>
			<view class="itembox" v-if="info.remark" >
				<view class="name">
					备注:
				</view>
				<view class="inputBox">
					<input type="text" v-model="info.remark" placeholder="请输入备注" />
				</view>
			</view>
			
		</view>
		<view class="" style="height: 220rpx;">
		</view>
		<view class="bottom-box" style="z-index: 99;">
			<view class="bnt" @click="addBnt()">
				确定
			</view>
		</view>
		<!-- 车间选择弹窗 -->
		<uni-popup ref="areasPopup" type="center" >
			<view class="areasbox-box">
				<view class="areasbox">
					<picker-view :indicator-style="indicatorStyle" :value="areaValue" @change="bindChange"
						class="picker-views">
						<picker-view-column>
							<view class="items" v-for="(item,index) in dataTree" :key="index">{{item.label}}</view>
						</picker-view-column>
						<picker-view-column>
							<view class="items" v-for="(item,index) in nameList" :key="index">{{item.label}}</view>
						</picker-view-column>
					</picker-view>
				</view>
				<view class="bom-bnt">
					<view class="cancel" @click="areasPopupClose()">
						取消
					</view>
					<view class="addbnt" @click="areasAddBnt()">
						确定
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import * as index from '@/api/index.js'
	export default {
		data() {
			return {
				info:{},
				selectCheName: '', //选择的车间名称
				selectCheId: '', //选择的车间id
				selectName: '', // 选择的人员名称
				selectId: '', // 选择的人员id
				workerList: [], // 工人列表
				selectedWorker: [{
					selectCheName: '',
					selectCheId: '',
					selectName: '',
					selectId: ''
				}], // 当前选中的工人
				dataTree: [],
				nameList: [],
			}
		},
		onLoad(e) {
			this.info = JSON.parse(e.info)
			this.selectedWorker[0].selectName =  JSON.parse(e.info).userName
			this.selectedWorker[0].selectCheName = JSON.parse(e.info).deptName
			this.deptTreeselect()
			// this.getInfo()
		},
		methods: {
			// 打开选择工人
			areasPopupBnt(e) {
				if (!this.selectId) {
					this.selectCheName = this.dataTree[0].label
					this.selectCheId = this.dataTree[0].id
					this.selectName = this.nameList[0].label
					this.selectId = this.nameList[0].value
				}
				this.$refs.areasPopup.open()
			},
			// 关闭选择工人
			areasPopupClose() {
				this.$refs.areasPopup.close()
			},
			// 确定选择弹窗
			areasAddBnt() {
				this.selectedWorker[0].selectCheName = this.selectCheName
				this.selectedWorker[0].selectCheId = this.selectCheId
				this.selectedWorker[0].selectName = this.selectName
				this.selectedWorker[0].selectId = this.selectId
				this.$refs.areasPopup.close()
			},
			deptTreeselect() {
				index.deptTreeselect({
					parentId: '57d779853f0f4d14b03d51db30099456'
				}).then(res => {
					this.dataTree = res.data
					this.deptName(this.dataTree[0].id)
				})
			},
			deptName(e) {
				index.getUserByDept({
					deptId: e,
					pageNum: 1,
					pageSize: 100
				}).then(res => {
					this.nameList = res.data.nameList
				})
			},
			bindChange: function(e) {
				console.log(e.detail.value, 'e.detail.value')
				this.selectCheName = this.dataTree[e.detail.value[0]].label
				this.selectCheId = this.dataTree[e.detail.value[0]].id
				this.selectName = this.nameList[e.detail.value[1]].label
				this.selectId = this.nameList[e.detail.value[1]].value
				this.deptName(this.dataTree[e.detail.value[0]].id)
				this.$forceUpdate()
			},
			onKeyInput (event) {
				this.info.count = event.target.value
				this.info.allMoney = event.target.value*this.info.money
			},
			addBnt(e) {
				// if(this.info.remainCount > this.info.count) {
				// 	this.$tools.toast("请选择工人!");
				// 	return;
				// }
				uni.showModal({
					title: '提示',
					content: '确定提交吗？',
					success :(res)=> {
						if (res.confirm) {
							if(this.info.userName == this.selectedWorker[0].selectName ) {
								index.goodsSaledoGongxuUserEdit(this.info).then(res=>{
									setTimeout(res=>{
										uni.navigateBack()
									},100)
								})
							} else {
								this.info.userId =  this.selectId
								this.info.userName = this.selectName 
								index.addUserTaskZhuanpai(this.info).then(res=>{
									setTimeout(res=>{
										uni.navigateBack()
									},100)
								})
							}
							
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
		}

	}
</script>

<style lang="scss">
	.content {
		padding-left: 20rpx;
		padding-right: 20rpx;
		.form {
			margin-top: 20rpx;
			padding: 20rpx;
			background-color: #ffffff;
			border-radius: 10rpx;
			.titel {
				padding-top: 30rpx;
				padding-bottom: 30rpx;
				color: #333;
				font-size: 28rpx;
				font-weight: bold;
			}
			.itembox {
				border-top: 1px solid #EDEDED;
				display: flex;
				align-items: center;
				padding-top: 30rpx;
				padding-bottom: 30rpx;
				.name {
					text-align: right;
					width: 160rpx;
					color: #999;
					font-size: 28rpx;
				}
				.inputBox {
					padding-left: 20rpx;
					width: 600rpx;
					color: #333;
					font-size: 28rpx;
				}
			}
		}
		.bottom-box {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 750rpx;
			padding: 30rpx;
			padding-bottom: 120rpx;
			height: 120rpx;
			background: #ffffff;
			display: flex;
			justify-content: center;
			.bnt {
				color: #ffffff;
				width: 306rpx;
				height: 88rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				border-radius: 10rpx;
				background: #4D8987;
			}
		}
		.areasbox-box {
			background-color: #FFFFFF;
			z-index: 99;
			.areasbox {
				width: 750rpx;
				height: 600rpx;
				.picker-views {
					width: 750rpx;
					height: 600rpx;
					margin-top: 20rpx;
				}
			
				.items {
					margin-left: 3rpx;
					font-size: 26rpx;
					text-overflow: ellipsis;
					overflow: hidden;
					white-space: nowrap;
					line-height: 100rpx;
					text-align: center;
				}
			
			}
			
			.bom-bnt {
				width: 750rpx;
				display: flex;
				align-items: center;
				height: 100rpx;
				background-color: #FFFFFF;
				border-top: 1rpx solid #EAEAEA;
			
				.cancel {
					width: 50%;
					font-size: 32rpx;
					color: #333;
					text-align: center;
					line-height: 100rpx;
					border-right: 1rpx solid #EAEAEA;
				}
			
				.addbnt {
					width: 50%;
					text-align: center;
					line-height: 100rpx;
					color: #4D8987;
					font-size: 32rpx;
					font-weight: bold;
				}
			}
		}
	}
</style>